package com.noname.lib_base_java.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiManager;
import android.os.Build;

import java.util.List;

/**
 * Author: Lmoumou
 * Date: 2018-12-10 10:45
 * 文件名: NetworkConnectionUtils
 * 描述:wifi链接工具
 */
public class NetworkConnectionUtils {

    /**
     * 链接指定
     */
    @SuppressLint("MissingPermission")
    public static boolean connectToSocketWifi(WifiManager manager, String wifiSSID) {
        WifiConfiguration wifiConfiguration = new WifiConfiguration();
        wifiConfiguration.SSID = "\"" + wifiSSID + "\"";
        wifiConfiguration.allowedKeyManagement.set(KeyMgmt.NONE);
        //小米手机MIUI7/华为EMUI4.1 需要webKey
        wifiConfiguration.wepKeys[0] = "\"" + "\"";
        int networkId = manager.addNetwork(wifiConfiguration);
        if (networkId != -1) {
            manager.enableNetwork(networkId, true);
            return true;
        } else {
            WifiConfiguration wifiConfiguration2 = new WifiConfiguration();
            wifiConfiguration2.SSID = "\"" + wifiSSID + "\"";

            wifiConfiguration2.allowedKeyManagement.set(KeyMgmt.NONE);
            networkId = manager.addNetwork(wifiConfiguration2);
            if (networkId != -1) {
                manager.enableNetwork(networkId, true);
                return true;
            }
        }
        return false;
    }

    /**
     * 获取连接的wifi节点各个配置选项的加密类型
     *
     * @param manager
     * @param ssid
     * @param password
     * @return
     */
    @SuppressLint("MissingPermission")
    public static WifiConfiguration getWifiConfiguration(WifiManager manager, String ssid, String password) {

        WifiConfiguration wifiConfiguration = new WifiConfiguration();
        wifiConfiguration.SSID = "\"" + ssid + "\"";
        List<ScanResult> list = manager.getScanResults();
        for (ScanResult scResult : list) {
            if (ssid == scResult.SSID) {
                String capabilities = scResult.capabilities;
                if (capabilities.contains("WEP") || capabilities.contains("wep")) {
                    wifiConfiguration.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
                    wifiConfiguration.preSharedKey = "\"" + password + "\"";
                } else if (capabilities.contains("WPA") || capabilities.contains("wpa")) {
                    wifiConfiguration.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
                    wifiConfiguration.preSharedKey = "\"" + password + "\"";
                } else {
                    wifiConfiguration.allowedKeyManagement.set(KeyMgmt.NONE);
                }
            }
        }
        return wifiConfiguration;
    }


//    /**
//     * 给温控器成功发送联网命令后，链接温控器链接的wifi节点
//     *
//     * @param strRouterSSID
//     * @return
//     */
//    public static void connectWifiSSID(Context context, WifiManager manager, String ssid, String password) {
//        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
//            WifiAutoConnectManager
//        }
//    }

//    /**
//     * 格式化格式化RouterSSID
//     *
//     * @param strRouterSSID
//     * @return
//     */
//    public static String formatRouterSSID(String strRouterSSID){
//        if (strRouterSSID.contains("\"")){
//            strRouterSSID=strRouterSSID.replace("\"".toRegex(), "");
//        }
//    }

}
